﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RCREEE.Library.Entities;
using System.Data;
using RCREEE.Library;

namespace RCREEE.Web
{
    public partial class StrategicObjective : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //// Add language code to the session object
            //Session.Add("LanguageCode", 1033);

            //EnableControls(false);
            ShowControls(false);
            try
            {
                dvDetails.AutoGenerateEditButton = CanEdit(Convert.ToInt32(ddlCountries.SelectedValue));
            }
            catch (Exception exc)
            { }
        }

        protected void dvDetails_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            e.KeepInEditMode = false;
            dvDetails.ChangeMode(DetailsViewMode.ReadOnly);
        }

        protected void dvDetails_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            dvDetails.ChangeMode(e.NewMode);

            if (e.NewMode != DetailsViewMode.Insert)
            {
                BindControls();
            }

            //if (e.NewMode == DetailsViewMode.ReadOnly)
            //    EnableControls(false);
            //else
            //    EnableControls(true);
        }

        protected void dvDetails_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            StrategicObjectives _strategicObjective = new StrategicObjectives();

            _strategicObjective.ItemId = Convert.ToInt32(lstStrategicObjectives.SelectedValue);
            _strategicObjective.CountryId = Convert.ToInt32(ddlCountries.SelectedValue);
            _strategicObjective.UnitId = Convert.ToInt32(ddlUnits.SelectedValue);
            _strategicObjective.IsEE = rdbIsEE.Checked;
            _strategicObjective.IsRE = rdbIsRE.Checked;

            foreach (DetailsViewRow dvRow in dvDetails.Rows)
            {
                if (dvRow.Cells[0].Text != "")
                {
                    StrategicObjectivesItemValue tempItem = new StrategicObjectivesItemValue();
                    tempItem.Year = Convert.ToInt32(dvRow.Cells[0].Text);
                    if (((TextBox)dvRow.Cells[1].Controls[0]).Text != "")
                        tempItem.Value = Convert.ToSingle(((TextBox)dvRow.Cells[1].Controls[0]).Text);
                    else
                        tempItem.Value = -1;
                    _strategicObjective.ItemValues.Add(tempItem);
                }
            }

            _strategicObjective.UpdateStrategicObjectiveItemValues();

            e.Cancel = true;
            dvDetails.ChangeMode(DetailsViewMode.ReadOnly);
            BindControls();
        }

        private void BindControls()
        {
            if ((ddlCountries.SelectedValue != null && ddlCountries.SelectedValue != "0")
                && (lstStrategicObjectives.SelectedValue != null && lstStrategicObjectives.SelectedValue != ""))
            {
                ShowControls(true);

                StrategicObjectives _strategicObjective = DataManager.GetOneStrategicObjectiveItemValuesById(Convert.ToInt32(lstStrategicObjectives.SelectedValue), Convert.ToInt32(ddlCountries.SelectedValue));


                if (_strategicObjective.UnitId != 0)
                    ddlUnits.SelectedValue = _strategicObjective.UnitId.ToString();

                rdbIsEE.Checked = _strategicObjective.IsEE;
                rdbIsRE.Checked = _strategicObjective.IsRE;

                if (!_strategicObjective.IsEE && !_strategicObjective.IsRE)
                    rdbIsRE.Checked = true;

                DataTable dtItemValues = new DataTable();

                for (int i = 0; i < _strategicObjective.ItemValues.Count; i++)
                {
                    dtItemValues.Columns.Add(_strategicObjective.ItemValues[i].Year.ToString(), typeof(float));
                }

                DataRow dr = dtItemValues.NewRow();
                for (int i = 0; i < _strategicObjective.ItemValues.Count; i++)
                {
                    if (_strategicObjective.ItemValues[i].Value != -1)
                        dr[_strategicObjective.ItemValues[i].Year.ToString()] = _strategicObjective.ItemValues[i].Value;
                }

                dtItemValues.Rows.Add(dr);

                dvDetails.DataSource = dtItemValues;
                dvDetails.DataBind();

                try
                {
                    if (dvDetails.CurrentMode == DetailsViewMode.ReadOnly)
                        EnableControls(false);
                    else
                        EnableControls(true);
                }
                catch (Exception)
                {

                }

            }
            else
            {
                dvDetails.DataSource = null;
                dvDetails.DataBind();
            }
        }

        protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindControls();
        }

        protected void lstStrategicObjectives_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindControls();
        }

        private void EnableControls(bool Enable)
        {
            ddlUnits.Enabled = Enable;
            rdbIsEE.Enabled = Enable;
            rdbIsRE.Enabled = Enable;
        }

        private void ShowControls(bool Show)
        {
            lblUnit.Visible = Show;
            ddlUnits.Visible = Show;
            lblType.Visible = Show;
            rdbIsEE.Visible = Show;
            rdbIsRE.Visible = Show;
        }

        public bool CanEdit(int CountryId)
        {
            try
            {
                DataRow[] dr = ((DataTable)Session["Permissions"]).Select("CountryId = " + CountryId.ToString());

                if (dr.Length > 0)
                {
                    return Convert.ToBoolean(dr[0]["CanUpdate"]);
                }
                else
                    return false;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}